﻿////////////////////////////////////////////////////////////////////////////
// bl_GameManager
//
//
//                    Lovatto Studio 2016
////////////////////////////////////////////////////////////////////////////
using UnityEngine;
using UnityEngine.UI;

public class bl_GameManager : Singleton<bl_GameManager>
{
    [HideInInspector]public bool isPlaying = false;

    [Header("References")]
    [SerializeField]private Animator MainAnim;
    [SerializeField]private Image AudioIconImage;
    [SerializeField]private Sprite AudioOnSprite;
    [SerializeField]private Sprite AudioOffSprite;
    [SerializeField]private Animator GameOverAnim;
    [SerializeField]private GameObject PlayButton;//we need show only one time this, so will desactive after use it.
    [SerializeField]private GameObject[] ExtraDefenses;
    public Transform FloatingParent;

    [Header("Backgrounds")]
    [SerializeField]private SpriteRenderer BackgroundRender;
    [SerializeField]private Color[] BackgroundsColors;

    private bool audioOn = true;
    public int lastPoint = 0; //记录复活之前的分数
    /// <summary>
    /// 
    /// </summary>
    void Start()
    {
        LoadSettings();
        ChangeBackground();
    }

    /// <summary>
    /// 
    /// </summary>
    void LoadSettings()
    {
        audioOn = bl_Utils.PlayerPrefsX.GetBool(KeyMaster.AudioEnable, true);
        AudioIconImage.sprite = (audioOn) ? AudioOnSprite : AudioOffSprite;
        AudioListener.pause = !audioOn;

        MainAnim.gameObject.SetActive(true);
        MainAnim.SetBool("show", true);
    }

    /// <summary>
    /// 
    /// </summary>
    public void Share()
    {
        StartCoroutine(bl_LovattoMobileUtils.TakeScreenShotAndShare(0));
    }

    /// <summary>
    /// 
    /// </summary>
    public void OnGameOver()
    {
        isPlaying = false;
        PlayButton.SetActive(false);
        MainAnim.gameObject.SetActive(true);
        MainAnim.SetBool("show", true);
        GameOverAnim.gameObject.SetActive(true);
        GameOverAnim.SetBool("show", true);
        bl_SpawnerManager.Instance.HideAll();
        bl_ScoreManager.Instance.Reset();
    }


    /// <summary>
    /// /
    /// </summary>
    public void TryAgain()
    {
        if (isPlaying)
            return;

        bl_TimeManager.Instance.SetSlowMotion(false);
        MainAnim.SetBool("show", false);
        StartCoroutine(bl_Utils.AnimatorUtils.WaitAnimationLenghtForDesactive(MainAnim));
        GameOverAnim.SetBool("show", false);
        StartCoroutine(bl_Utils.AnimatorUtils.WaitAnimationLenghtForDesactive(GameOverAnim));
        //ChangeBackground();
        bl_Planet.Instance.Reset();
        bl_SpawnerManager.Instance.Spawn();
        foreach(GameObject g in ExtraDefenses) { g.SetActive(true); }

        isPlaying = true;
    }

    public void LookVedio()
    {
        if (isPlaying)
            return;

        string videoAdid = VideData.Instance.GetVide();
        TTSDK.TTRewardedVideoAd r = AdMgr.Instance.ShowVideoAdNew(videoAdid);
        //创建激励视频关闭回调
        r.OnClose += (bol, val) =>
        {
            Debug.Log("关闭广告 ，是否已经看完" + bol);
            //看完广告获得提示
            if (bol)
            {

                //复活  重置分数
                bl_TimeManager.Instance.SetSlowMotion(false);
                MainAnim.SetBool("show", false);
                StartCoroutine(bl_Utils.AnimatorUtils.WaitAnimationLenghtForDesactive(MainAnim));
                GameOverAnim.SetBool("show", false);
                StartCoroutine(bl_Utils.AnimatorUtils.WaitAnimationLenghtForDesactive(GameOverAnim));
                //ChangeBackground();
                bl_Planet.Instance.Reset();
                bl_SpawnerManager.Instance.Spawn();
                foreach (GameObject g in ExtraDefenses) { g.SetActive(true); }

                isPlaying = true;

                //恢复原始分数
                bl_ScoreManager.Instance.AddScore(lastPoint);

                MaidianNew.MaidianBackCallback(" ");
                Debug.Log("广告看完");
            }
            else
            {
                Debug.Log("广告未看完");
            }
        };

        //创建报错回调  
        r.OnError += (code, errorMessage) =>
        {

            Debug.Log("code:" + code + "错误信息：" + errorMessage);
        };

        //展示广告（新版本必须调用，不然无法展示出来广告）
        r.Show();
    }


   
    
    void ChangeBackground()
    {
        //BackgroundRender.color = BackgroundsColors[Random.Range(0, BackgroundsColors.Length)];
    }

    /// <summary>
    /// 
    /// </summary>
    public void StartGame()
    {
        isPlaying = true;
    }

    /// <summary>
    /// 
    /// </summary>
    public void SwitchAudio()
    {
        audioOn = !audioOn;
        AudioIconImage.sprite = (audioOn) ? AudioOnSprite : AudioOffSprite;
        AudioListener.pause = !audioOn;
        bl_Utils.PlayerPrefsX.SetBool(KeyMaster.AudioEnable, audioOn);
    }

    public static bl_GameManager Instance
    {
        get
        {
            return ((bl_GameManager)mInstance);
        }
        set
        {
            mInstance = value;
        }
    }
}